//
//  BCWebImageManager.m
//  BCWebImageKit
//
//  Created by Basic on 2019/9/3.
//

#import "BCWebImageManager.h"

// SDWEBIMAGE_INNER，表示是否使用内嵌的sdwebimage，在podspec里定义
#ifdef SDWEBIMAGE_INNER
#import "SDWebImageOperation.h"
#import "SDWebImageManager.h"
#import "SDImageCache.h"
#else
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunknown-warning-option"
#pragma clang diagnostic ignored "-Wstrict-prototypes"
#import <SDWebImage/SDWebImageOperation.h>
#import <SDWebImage/SDWebImageManager.h>
#import <SDWebImage/SDImageCache.h>
#pragma clang diagnostic pop
#endif

@interface BCWebImageManager()

@end

@implementation BCWebImageManager

//MARK: - life cycle


//MARK: - 下载图片
+ (id<BCWebImageOperation>)loadImageWithURLStr:(NSString *)urlStr withCompletion:(void (^)(UIImage * _Nullable, NSData * _Nullable, BOOL))completedBlock {
    return (id<BCWebImageOperation> )[[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:urlStr] options:SDWebImageHighPriority progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        if (completedBlock) {
            completedBlock(image, data, finished);
        }
    }];
}
+ (id<BCWebImageOperation> )loadImageWithURL:(NSURL *)url options:(BCWebImageOptions)options progress:(void (^)(NSInteger, NSInteger, NSURL * _Nullable))progressBlock completed:(void (^)(UIImage * _Nullable, NSData * _Nullable, NSError * _Nullable, BCImageCacheType, BOOL, NSURL * _Nullable))completedBlock {
    return (id<BCWebImageOperation> )[[SDWebImageManager sharedManager] loadImageWithURL:url options:(SDWebImageOptions)options progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
        if (completedBlock) {
            completedBlock(image, data, error, (BCImageCacheType)cacheType, finished, imageURL);
        }
    }];
}

//MARK: - 取消下载
+ (void)cancelAll {
    [[SDWebImageManager sharedManager] cancelAll];
}

//MARK: - 缓存
+ (NSUInteger )getSize {
    return SDImageCache.sharedImageCache.totalDiskSize;
}
+ (void)clearMemory {
    [SDImageCache.sharedImageCache clearMemory];
}
+ (void)clearDisk:(void(^_Nullable)(void) )completion {
    [SDImageCache.sharedImageCache clearDiskOnCompletion:^{
        if (completion) {
            completion();
        }
    }];
}
@end
